/*
 * Copyright (c) 2010 MoSync AB
 *
 * Permission is hereby granted, free of charge, to any person obtaining a copy
 * of this software and associated documentation files (the "Software"), to deal
 * in the Software without restriction, including without limitation the rights
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 * copies of the Software, and to permit persons to whom the Software is
 * furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice shall be included in
 * all copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
 * THE SOFTWARE.
 */

#ifndef BROWSERSCREEN_H
#define BROWSERSCREEN_H

#include <MAUtil/Map.h>
#include <MAUtil/Moblet.h>
#include <MAUI/Screen.h>
//#include <MAUI/ListBox.h>
#include <MAInterpreter/LuaInterpreter.h>
#include "BrowserMoblet.h"
#include "MyTextBoxListener.h"

using namespace MAUI;
using namespace MAUtil;
using namespace MAInterpreter;

class BrowserScreenDownloader;

// Number of characters that can be received from maTextBox.
#define TEXTBOX_RESULT_CAPACITY 256

class BrowserScreen : public Screen, WidgetListener, MyTextBoxListener
{
public:
	// Create and delete.
	BrowserScreen(BrowserMoblet* moblet);
	~BrowserScreen();

	// Events.
	void keyPressEvent(int keyCode, int nativeCode);
	void pointerPressEvent(MAPoint2d point);
	void selectionChanged(Widget *widget, bool selected);
	void show();
	void hide();
	void selectPrevious();
	void selectNext();
	void openTextBox();
	void handleTextBoxResult(int textBoxResultLength);
	void runSelectedScript();
	void runUrlEditBoxScript();
	void runScriptListBoxScript();
	void runScript(const String& url);
	void exitApp();

	// User interface.
	Layout* createMainLayout();
	Widget* createTitleBar();
	Widget* createNavigationBox();
	Widget* createScriptBox();
	Widget* createButtonBar();
	void addNavigationItem(const String& title, const String& url);
	Widget* getUrlEditBox(Widget* navigationBox);
	Widget* getScriptListBox(Widget* scriptBox);
	Widget* getExitButton(Widget* buttonBar);
	Widget* getRunButton(Widget* buttonBar);
	Widget* getMenuButton(Widget* buttonBar);
	void setPadding(Widget *w);
	void setTitlePadding(Widget *w);
	void setButtonPadding(Widget *w);

private:
	BrowserMoblet* moblet;
	Widget* mainLayout;
	Widget* urlEditBox;
	Widget* scriptTextBox;
	Widget* scriptListBox;
	Widget* runButton;
	Widget* menuButton;
	Widget* exitButton;
	Map<int, String*> urls;
	wchar textBoxResult[TEXTBOX_RESULT_CAPACITY];
};

#endif
